﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.WorkFlow_Run_Processs.Authorization;
using LeadThing.AbpZeroTemplate.WorkFlow_Run_Processs.Dtos;
using LeadThing.AbpZeroTemplate.DataExporting.Excel.EpPlus;
using Abp.Timing.Timezone;
using Abp.Runtime.Session;
using LeadThing.AbpZeroTemplate.Dto;

namespace LeadThing.AbpZeroTemplate.WorkFlow_Run_Processs
{
    /// <summary>
    /// 实例节点流转明细的导出EXCEL功能的实现
    /// </summary>
    public class WorkFlow_Run_ProcessListExcelExporter : EpPlusExcelExporterBase, IWorkFlow_Run_ProcessListExcelExporter
    {

        private readonly ITimeZoneConverter _timeZoneConverter;
        private readonly IAbpSession _abpSession;


        /// <summary>
        /// 构造方法
        /// </summary>
        public WorkFlow_Run_ProcessListExcelExporter(ITimeZoneConverter timeZoneConverter, IAbpSession abpSession)
        {
            _timeZoneConverter = timeZoneConverter;
            _abpSession = abpSession;
        }



        /// <summary>
        /// 导出实例节点流转明细到EXCEL文件
        /// <param name="workFlow_Run_ProcessListDtos">导出信息的DTO</param>
        /// </summary>
        public FileDto ExportWorkFlow_Run_ProcessToFile(List<WorkFlow_Run_ProcessListDto> workFlow_Run_ProcessListDtos)
        {


            var file = CreateExcelPackage("workFlow_Run_ProcessList.xlsx", excelPackage =>
            {

                var sheet = excelPackage.Workbook.Worksheets.Add(L("WorkFlow_Run_Process"));
                sheet.OutLineApplyStyle = true;

                AddHeader(
                    sheet,
                     L("RunID"),
                     L("FlowID"),
                     L("ProessID"),
                     L("ParentProessID"),
                     L("IsBack"),
                     L("JS_Time"),
                     L("BL_Time"),
                     L("JJ_Time"),
                     L("Status"),
                     L("YBLR"),
                     L("BLR"),
                     L("PXM"),
                     L("CreationTime")

                    );
                AddObjects(sheet, 2, workFlow_Run_ProcessListDtos,

             _ => _.RunID,

             _ => _.FlowID,

             _ => _.ProessID,

             _ => _.ParentProessID,

             _ => _.IsBack,

        _ => _timeZoneConverter.Convert(_.JS_Time, _abpSession.TenantId, _abpSession.GetUserId()),
        _ => _timeZoneConverter.Convert(_.BL_Time, _abpSession.TenantId, _abpSession.GetUserId()),
        _ => _timeZoneConverter.Convert(_.JJ_Time, _abpSession.TenantId, _abpSession.GetUserId()),
             _ => _.Status,

             _ => _.YBLR,

             _ => _.BLR,

             _ => _.PXM,

        _ => _timeZoneConverter.Convert(_.CreationTime, _abpSession.TenantId, _abpSession.GetUserId())
       );
                //写个时间转换的吧
                //var creationTimeColumn = sheet.Column(10);
                //creationTimeColumn.Style.Numberformat.Format = "yyyy-mm-dd";

                for (var i = 1; i <= 13; i++)
                {
                    sheet.Column(i).AutoFit();
                }

            });
            return file;

        }

    }
}
